package com.jarvis.hospital.prescription;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jarvis.hospital.R;
import com.jarvis.hospital.SettingActivity;
import com.jarvis.hospital.common.BaseActivity;
import com.jarvis.hospital.login.model.LoginModel;
import com.jarvis.hospital.login.model.UserModel;
import com.jarvis.hospital.prescription.adapter.BloodDoneFragmentAdapter;
import com.jarvis.hospital.prescription.adapter.PrescriptionFragmentAdapter;
import com.jarvis.hospital.util.SharePreferenceKey;
import com.jarvis.hospital.util.SharePreferenceUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class BloodDoneListActivity extends BaseActivity implements View.OnClickListener {
    private List<Fragment> fragments = new ArrayList<>();
    private BloodDoneFragmentAdapter adapter;

    private BloodFragment fragment1;
    private BloodFragment fragment2;
    private BloodFragment fragment3;

    @BindView(R.id.tab)
    TabLayout tab;
    @BindView(R.id.tab_content)
    ViewPager tab_content;

    @BindView(R.id.rel_left)
    RelativeLayout rel_left;
    @BindView(R.id.rel_right)
    RelativeLayout rel_right;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_right)
    TextView tv_right;

    @Override
    public int getLayoutId() {
        return R.layout.activity_all_prescription_list;
    }

    @Override
    public void initData() {
        Bundle bundle = this.getIntent().getExtras();
        if(bundle!=null){
            boolean isAuto = bundle.getBoolean("isAuto");
            if (isAuto) {
//            UpdateManager manager = new UpdateManager(this);
//            manager.checkUpdate();
            }
        }
        fragment1 = BloodFragment.newInstance(1);
        fragment2 = BloodFragment.newInstance(2);
        fragment3 = BloodFragment.newInstance(0);

        fragments.add(fragment1);
        fragments.add(fragment2);
        fragments.add(fragment3);
        adapter = new BloodDoneFragmentAdapter(this.getSupportFragmentManager(), fragments);
        tab_content.setAdapter(adapter);
        tab.setupWithViewPager(tab_content);
        tab.setTabMode(TabLayout.MODE_FIXED);//tab不能滚动，平分屏幕宽度
        LinearLayout linearLayout = (LinearLayout) tab.getChildAt(0);
        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        linearLayout.setDividerPadding(40);
        linearLayout.setDividerDrawable(ContextCompat
                .getDrawable(this,
                        R.drawable.layout_divider_vertical));
        tab_content.setOffscreenPageLimit(2);

        UserModel userModel = SharePreferenceUtils.getObject(SharePreferenceKey.LOGIN_MODEL, LoginModel.class).getUserInfo();
        String userRole = userModel.getUserRole();
        if("docimaster".equals(userRole)){
            rel_left.setVisibility(View.GONE);
            tv_right.setText("设置");
            rel_right.setVisibility(View.VISIBLE);
            rel_right.setOnClickListener(this);
        }else {
            rel_left.setVisibility(View.VISIBLE);
            rel_right.setVisibility(View.GONE);
            rel_left.setOnClickListener(this);
        }

    }

    @Override
    public void initView() {
        tv_title.setText("采血任务表");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        fragment1.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rel_left:
                finish();
                break;
            case R.id.rel_right:
                startActivity(SettingActivity.class);
                break;
        }
    }
}
